/*******************************************************************************
* This file provides the API for AT26DF081A serial flash memory.
* These API must only be used after SPI 1 has been initialized.
*
* Author: Kong Wai Weng
* Email:  waiweng83@gmail.com
*******************************************************************************/



#ifndef _HAL_SERIALFLASH_H
#define _HAL_SERIALFLASH_H



#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "system.h"
#include "hal_spi.h"
#include "variables.h"



/*******************************************************************************
* PUBLIC FUNCTION PROTOTYPES                                                   *
*******************************************************************************/

/*******************************************************************************
* PUBLIC FUNCTION: vSFlashChipErase
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Erase the entire memory.
* This is a blocking function and should not be called from ISR.
*
*******************************************************************************/
void vSFlashChipErase(void);



/*******************************************************************************
* PUBLIC FUNCTION: vSFlashRead
*
* PARAMETERS:
* ~ ulAddress	- The source address (0x0 - 0xFFFFF).
* ~ uiLength	- Number of bytes to read.
* ~ pucBuffer	- Buffer for the data.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Read from the serial flash.
* This is a blocking function and should not be called from ISR.
*
*******************************************************************************/
void vSFlashRead(unsigned long ulAddress, unsigned int uiLength, unsigned char* pucBuffer);



/*******************************************************************************
* PUBLIC FUNCTION: vSFlashWrite
*
* PARAMETERS:
* ~ ulAddress	- The destination address (0x0 - 0xFFFFF).
* ~ uiLength	- Number of bytes to write.
* ~ pucBuffer	- Buffer for the data.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Write to the serial flash.
* This is a blocking function and should not be called from ISR.
*
*******************************************************************************/
void vSFlashWrite(unsigned long ulAddress, unsigned int uiLength, unsigned char* pucBuffer);



/*******************************************************************************
* PUBLIC FUNCTION: ucSFlashReadStatus
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ Content of the status register.
*
* DESCRIPTIONS:
* Read the status register of the serial flash.
*
*******************************************************************************/
unsigned char ucSFlashReadStatus(void);



/*******************************************************************************
* PUBLIC FUNCTION: vSFlashWriteStatus
*
* PARAMETERS:
* ~ ucData	- Data we want to write.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Write the status register of the serial flash.
*
*******************************************************************************/
void vSFlashWriteStatus(unsigned char ucData);



/*******************************************************************************
* PUBLIC FUNCTION: vSFlashGlobalUnprotect
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Unprotect the whole memory of the serial flash.
*
*******************************************************************************/
void vSFlashGlobalUnprotect(void);



/*******************************************************************************
* PUBLIC FUNCTION: ucSFlashBusy
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ Return 0 if ready.
* ~ Return 1 if busy.
*
* DESCRIPTIONS:
* Check whether the serial flash is busy.
*
*******************************************************************************/
unsigned char ucSFlashBusy(void);



#endif
